package com.zt.yongxinbao.controller;

import com.zt.yongxinbao.dto.UserLoginParam;
import com.zt.yongxinbao.dto.UserRegistParam;
import com.zt.yongxinbao.entity.User;
import com.zt.yongxinbao.entity.UserDetail;
import com.zt.yongxinbao.service.UserDetailService;
import com.zt.yongxinbao.service.UserService;
import com.zt.yongxinbao.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UserService userService;
    private User user;
    @Autowired
    private UserDetailService userDetailService;
    private UserDetail userDetail;
    @PostMapping("/login")
//    @RequestMapping("/login")
    public CommonResult login(@RequestBody UserLoginParam userLoginParam){
        String token = userService.login(userLoginParam.getUsername(), userLoginParam.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
    @PostMapping("/regist")
    public CommonResult regist(@RequestBody UserRegistParam userRegistParam){
        Integer registVaild=userService.registVaild(userRegistParam);

        if (registVaild==1){
            return CommonResult.failed("用户名已被使用，请重试");
        }else if (registVaild==2){
            return CommonResult.failed("邮箱已被绑定其他账号，请重试");
        }else if (registVaild==0) {
            user = userService.regist(userRegistParam);
            if (user == null) return CommonResult.failed("网络繁忙，请稍后再试");
        }
        return CommonResult.success(user);
    }
//    退出登录
    @PostMapping(value="/logout")
    public  CommonResult logout(){
        return CommonResult.success("您已退出登录");
    }
//    获取用户信息
    @GetMapping (value = "/getUser/{username}")
    public CommonResult getUser(@PathVariable String username){
        return CommonResult.success(userService.getUserByUsername(username));
    }
//    获取用户详细信息
    @GetMapping (value = "/getUserInfo/{username}")
    public CommonResult getUserInfo(@PathVariable String username){
        return CommonResult.success(userDetailService.getUserDetail(username));
    }
//    更改头像
    @PostMapping (value = "/updateUserAvatar/{username}")
    public  CommonResult updateUserAvatar(@PathVariable String username, @RequestParam String avatar){
        if (userDetailService.updateUserAvatar(username,avatar)) return  CommonResult.success("修改头像成功");
        return CommonResult.failed("修改头像失败，请重试！");
    }
//    更改真实姓名
    @PostMapping(value = "/updateUserActualName/{username}")
    public  CommonResult updateUserActualName(@PathVariable String username,@RequestParam String actualName ){
        if (userDetailService.updateUserActualName(username,actualName)) return  CommonResult.success("修改真实姓名成功");
        return CommonResult.failed("修改真实姓名失败，请重试！");
    }
//    更改手机号码
    @PostMapping(value = "/updateUserTelPhone/{username}")
    public  CommonResult updateUserTelPhone(@PathVariable String username,@RequestParam String telPhone ){
        if (userDetailService.updateUserTelPhone(username,telPhone)) return  CommonResult.success("修改手机号码成功");
        return CommonResult.failed("手机号码已绑定其他账号，请重试！");
    }
//    更改性别
    @PostMapping(value = "/updateUserSex/{username}")
    public  CommonResult updateUserSex(@PathVariable String username,@RequestParam String sex ){
        if (userDetailService.updateUserSex(username,sex)) return  CommonResult.success("修改性别成功");
        return CommonResult.failed("修改性别失败，请重试！");
    }
    //    更改邮箱
    @PostMapping(value = "/updateUserEmail/{username}")
    public  CommonResult updateUserEmail(@PathVariable String username,@RequestParam String email ){
        if (userService.updateEmail(username,email)) return  CommonResult.success("修改邮箱成功");
        return CommonResult.failed("邮箱已绑定其他账号，请重试！");
    }
//    更改密码
    @PostMapping(value = "/updateUserPassword/{username}")
    public  CommonResult updateUserPassword(@PathVariable String username,@RequestParam String password ){
        if (userService.updatePassword(username,password)) return  CommonResult.success("修改密码成功");
        return CommonResult.failed("修改密码失败，请重试！");
    }
//    确认邮箱
    @PostMapping(value = "confrimEmail/{email}")
    public  CommonResult confrimEmail(@PathVariable String email){
        return CommonResult.success(userService.sendMall(email));
    }
}
